﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using OpenComposite.Base.Collections;
using System.Globalization;
using OpenComposite.Repository.Properties;

namespace OpenComposite.EII.RepositoryUIs
{
	public partial class UCKPI : RepositoryItemUI
	{
		public UCKPI(IServiceProvider serviceProvider)
			: base(serviceProvider)
		{
			InitializeComponent();
			init();
		}

		private void init()
		{
			_kpis = Global.Data.GetKeyPerformanceIndicatiors();
			_kpis.Insert(0, new KeyPerformanceIndicator(false));
			_kpis[0].Name = Resources.sNewKPI;
			cmbKPI.DataSource = _kpis;
		}

		public override IRepositoryItem RepositoryItem
		{
			get
			{
				if ( _metric != null ) {
					save();
				}
				return _metric;
			}
			set
			{
				if ( value is KPIMetric ) {
					_metric = value as KPIMetric;
					load();
				}
			}
		}

		public override string TypeText
		{
			get
			{
				return "KPI Metric";
			}
		}

		public override string TypeDescription
		{
			get
			{

				return "Defines the value of a KPI";
			}
		}

		private void load()
		{
			decimal value;
			decimal.TryParse(_metric.Value,
				NumberStyles.Float, CultureInfo.InvariantCulture, out value);
			txtValue.Text =
				Helpers.ConvertUnit(_metric.Value, _metric.Kpi.BaseUnit,
					_metric.Kpi.GetKpiUnit(_metric.Unit)).ToString(CultureInfo.InvariantCulture);

			//if ( _metric.Kpi.Name == "Response Time" ) {
			//    txtValue.Text = Helpers.ConvertTime(value, "ms", _metric.Unit).ToString(CultureInfo.InvariantCulture);
			//} else if ( _metric.Kpi.Name == "Availability" ) {
			//    txtValue.Text = Helpers.ConvertPercent(value, null, _metric.Unit).ToString(CultureInfo.InvariantCulture);
			//} else {
			//    txtValue.Text = _metric.Value;
			//}
			txtName.Text = _metric.Name;
			txtComment.Text = _metric.Description;
			cmbKPI.SelectedItem = _metric.Kpi;
			cmbUnitOfMeasure.Text = _metric.Unit;
		}

		private void save()
		{
			KeyPerformanceIndicator kpi = cmbKPI.SelectedItem as KeyPerformanceIndicator;
			if ( kpi == null || kpi.Name == Resources.sNewKPI ) return;
			decimal value;
			decimal.TryParse(txtValue.Text.Replace(',', '.'),
				NumberStyles.Float, CultureInfo.InvariantCulture, out value);
			_metric.Value = Helpers.ConvertUnit(value, (KpiUnit)cmbUnitOfMeasure.SelectedItem, kpi.BaseUnit).ToString(CultureInfo.InvariantCulture);

			_metric.Name = txtName.Text;
			_metric.Description = txtComment.Text;
			_metric.Kpi = kpi;
			KpiUnit unit = cmbUnitOfMeasure.SelectedItem as KpiUnit;
			if ( unit != null ) {
				_metric.Unit = unit.Name;
			}
		}
		private BindingListEx<KeyPerformanceIndicator> _kpis = new BindingListEx<KeyPerformanceIndicator>();
		private BindingListEx<string> _units = new BindingListEx<string>();
		private KPIMetric _metric = null;

		private void btnAddKPI_Click(object sender, EventArgs e)
		{
			KeyPerformanceIndicator kpi = cmbKPI.SelectedItem as KeyPerformanceIndicator;
			if ( kpi == null || kpi.Name == Resources.sNewKPI ) {
				IRepositoryItem parent = null;
				kpi = Global.Data.RepositoryService.GetNewItem<KeyPerformanceIndicator>(true, ref parent);
				if ( kpi != null ) {
					_kpis.Add(kpi);
				}
			} else {
				kpi.ShowEditItemForm();
			}
		}

		private void cmbKPI_SelectedValueChanged(object sender, EventArgs e)
		{
			KeyPerformanceIndicator kpi = cmbKPI.SelectedItem as KeyPerformanceIndicator;
			if ( kpi == null ) return;
			cmbUnitOfMeasure.SelectedIndex = -1;
			cmbUnitOfMeasure.DataSource = null;
			cmbUnitOfMeasure.DataSource = kpi.ExtendedProperties.Units;

		}

		private void txtName_KeyPress(object sender, KeyPressEventArgs e)
		{
			// valid for metrics
			//if ( e.KeyChar < (char)32 ) return;
			//e.Handled = !Helpers.IsValidCharForName(e.KeyChar);
		}

		private void cmbKPI_Format(object sender, ListControlConvertEventArgs e)
		{

		}

	}
}
